
angular.module('U9ImHd.controllers')

.controller('PortSettingCtrl', ['$scope', '$interval', '$state', 'AppManager', 'SerialPort', 'imanDialog', 'APPCONSTANTS',
    function ($scope, $interval, $state, AppManager, SerialPort, imanDialog, APPCONSTANTS) {
        $scope.data = {
            bindDevices: SerialPort.getExternalDevices(true),
            appVersion: APPCONSTANTS.appVersion
        };

        var _deviceList, _activeDevice, _interval, _times;

        SerialPort.getAllDevices().then(function (devices) {
            _deviceList = devices;
        });

        $scope.addDevice = function () {
            var externalDevices = SerialPort.getExternalDevices();
            if (externalDevices.length === 0) {
                imanDialog.toastBottom('没有需要设置串口的外接设备');
                return;
            }
            imanDialog.showList({
                title: '请选择外接设备',
                list: externalDevices,
                valueField: 'Device',
                displayField: 'Name'
            }).then(function (select) {
                if (checkAdded(select.Device)) {
                    imanDialog.toastBottom('该外接设备已添加');
                    return;
                }
                $scope.data.bindDevices.push(select);
            });
        };
        $scope.selectTestPort = function (device) {
            if (_activeDevice && (_activeDevice.Device === device.Device)) {
                imanDialog.toastBottom('正在检测当前串口');
                return;
            }
            if (!_deviceList) {
                imanDialog.toastBottom('没有可用的串口');
                return;
            }
            imanDialog.showList({
                title: '请选择检测串口',
                list: _deviceList,
                valueField: 'path',
                displayField: 'name',
                value: device.testPort
            }).then(function (select) {
                device.testPort = select.path;
                device.testName = select.name;
            });
        };
        $scope.deleteDevice = function (index, device) {
            $scope.data.bindDevices.splice(index, 1);
            if (device.Port) {
                SerialPort.bindSerialPort(device.Device, "");
            }
        };
        $scope.startObserve = function (device) {
            if (_activeDevice) {
                if (_activeDevice.Device === device.Device) {
                    if (_interval) {
                        $interval.cancel(_interval);
                    }
                    SerialPort.close();
                    _activeDevice.Tip = null;
                    _activeDevice = null;
                } else {
                    imanDialog.toastBottom('请等待其他串口检测完毕或手动停止其他串口检测');
                }
                return;
            }
            SerialPort.open({ port: device.testPort }).then(function () {
                device.Tip = '检测中(10秒)...';
                _activeDevice = device;
                _times = 10;
                _interval = $interval(function () {
                    _times--;
                    if (_times <= 0) {
                        SerialPort.close();
                        $interval.cancel(_interval);
                        _activeDevice.Tip = null;
                        _activeDevice = null;
                    } else {
                        _activeDevice.Tip = '检测中(' + _times + '秒)...';
                    }
                }, 1000);
            }, function () {
                imanDialog.toastBottom('打开检测串口失败，请使用其他串口');
            });
        };
        $scope.checkUpdate = function () {
            AppManager.checkUpdate(true);
        };
        $scope.goInstalledApp = function () {
            $state.go('installedapp');
        };

        $scope.$on('serialport.DataReceived', function (e, data) {
            if (_activeDevice) {
                SerialPort.bindSerialPort(_activeDevice.Device, _activeDevice.testPort, _activeDevice.testName).then(function () {
                    _activeDevice.Port = _activeDevice.testPort;
                    _activeDevice.PortName = _activeDevice.testName;

                    if (_interval) {
                        $interval.cancel(_interval);
                    }
                    SerialPort.close();
                    _activeDevice.Tip = null;
                    _activeDevice = null;
                });
            }
        });

        $scope.$on('$ionicView.afterLeave', function () {
            SerialPort.close();
        });

        function checkAdded (device) {
            var added = false;
            for (var i = $scope.data.bindDevices.length - 1; i >= 0; i--) {
                if ($scope.data.bindDevices[i].Device === device) {
                    added = true;
                    break;
                }
            }
            return added;
        }
    }
]);